<?php
$loader->requireOnce('ordo/Employee.class.php');
class C_HumanResources extends Controller {
    private $logger;

    function C_HumanResources($template_mod = "general") {
        parent::Controller();
        $this->logger = LoggerManager::getLogger('module.human_resources.controllers.C_HumanResources');
        $this->template_mod = $template_mod;
        $this->view->path="human_resources";
        $this->setLayout('admin');
    }
    
    public function actionManage() {
    	$this->assign('list', $this->actionListEmployee());
    	return $this->view->render('manage.html');
    }
    
    public function actionListEmployee() {
    	$this->assign('typeList', Celini::newOrdo('EmployeeType')->getAll());
    	$this->assign('deptList', Celini::newOrdo('Department')->listAll());
    	$this->assign('employeeList', $this->actionGetEmployeeList());
    	return $this->view->render('list.html');
    }        
    
    public function actionGetEmployeeList($userPage = false, $deptId = 0, $typeId = 0, $mapped = 'all') {
    	if (isSet($_GET['deptId'])) {
    		$deptId = $_GET['deptId'];
    	}
    	if (isSet($_GET['typeId'])) {
    		$typeId = $_GET['typeId'];
    	}
   		if (isSet($_GET['mapped'])) {
    		$mapped = $_GET['mapped'];
    	}
    	$this->assign('index', 1);
    	$this->assign('userPage', $userPage);
    	$this->assign('employeeList', Celini::newOrdo('Employee')->getEmployeeList($deptId, $typeId, $mapped));
    	return $this->view->render('employee_list.html');
    }
    
    // @Trung Nguyen 
    // The following 2 actions are used for user admin page
    public function actionListForUserPage() {                
        $this->assign('typeList', Celini::newOrdo('EmployeeType')->getAll());
    	$this->assign('deptList', Celini::newOrdo('Department')->listAll());
    	$this->assign('employeeList', $this->actionGetEmployeeList(true, 0,0,'no'));
    	$this->assign('accountCreated', 'no');
    	return $this->view->render('list.html');
    }
    
    public function actionGetEmployeeListForUserPage() {
        return $this->actionGetEmployeeList(true);
    }
    
    
    public function actionEditEmployee($personId = 0) {
    	if (isSet($_GET['employeeId'])) {
    		$personId = $_GET['employeeId'];
    	}
    	    	
		$employee = Celini::newORDO('Employee', $personId);

		$this->assign('employeeTypeList', Celini::newORDO('EmployeeType')->listAllParentType());			
		$this->assign('deptList', Celini::newOrdo('Department')->listAll());
		$this->assign('diplomaList', $employee->getDiplomaList(0, true));		
		
		$this->assign_by_ref('employee',$employee);
		
		$orders	=	L10nHelper::getYMDorder();
        $this->assign('datePart_0', $orders[0]);
        $this->assign('datePart_1', $orders[1]);
        $this->assign('datePart_2', $orders[2]);
		
		$dateObj = new DateObject();
		$dateObj = $dateObj->create($employee->get('date_of_birth'));
		$dob = array (
			'dd'	=> $dateObj->getDay(),
			'mm'	=> $dateObj->getMonth(),
			'yyyy'	=> $dateObj->getYear()
		);		
	
		$this->assign('dob', $dob);
		return $this->view->render('edit.html');
    }
    
	public function actionEditEmployeeAjax() {			
		$employee = $_POST['employee'];				
		$employeeOrdo = Celini::newORDO('Employee');
		if (isset($employee['person_id']) && strlen(trim($employee['person_id'])) > 0) {
			$employeeOrdo->setup($employee['person_id']);
			if (!$employeeOrdo->isPopulated()) {
			    return -1;
			}					
		} else {
			$employee['mapped'] = 'no';
		}	
		
		if ($employee['dob_mm'] == '') $employee['dob_mm'] = '00';
		if ($employee['dob_dd'] == '') $employee['dob_dd'] = '00';
		$employee['date_of_birth'] = $employee['dob_yyyy'] . '-' 
		                            . $employee['dob_mm'] . '-'
		                            . $employee['dob_dd'];
		$name = $_POST['employee']['name'];
		$name = Utils::sanitizeAndCaseTitle($name);
		$employeeOrdo->populateArray($employee);
		$employeeOrdo->set('name', $name);
		$employeeOrdo->persist();		
								
		return $employeeOrdo->get_id();
	}
    
}
?>